package a66weding.com.jiehuntong.activity.home.hotel.hotelnew;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.andview.refreshview.XRefreshView;
import java.util.List;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.ShoplistBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;

/**
 * Created by admin on 2016/9/12.
 */
public class HomeBussinesFragment1 extends Fragment implements OnNetResultListener{
    private HomeHotelActivity2 mHomeHotelActivity;
    private boolean islogin;
    private int memBerId;
    private BusnessAdapter adapter;
    private int category;
    private List<ShoplistBean.DataBean> shopListData;
    private XRefreshView xRefreshView;
    private ListView list;
    private boolean isDownRefresh = true;
    private int page = 1;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.activity_hometaocanfragment1,null);
        initRefreshView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mHomeHotelActivity = (HomeHotelActivity2) getActivity();
        islogin = ((GlobalApplication) mHomeHotelActivity.getApplication()).isLogin();
        if(islogin) {
            memBerId = DataWareHourse.getInstance().getmUser().getData().get(0).getI_Id();
        }
        category = ((HomeHotelActivity2)getActivity()).getCategoryId();
        page = 1;
        getVpRightFromNet(page);
    }

    private void initRefreshView(View view){
        xRefreshView = (XRefreshView) view.findViewById(R.id.help_xrefreshview);
        // 设置是否可以上拉加载
        xRefreshView.setPullLoadEnable(true);
        // 设置刷新完成以后，headerview固定的时间
        xRefreshView.setPinnedTime(1000);
        // 静默加载模式不能设置footerview
        // 设置支持自动刷新
        xRefreshView.setAutoLoadMore(true);
        list = (ListView) view.findViewById(R.id.hotel_list);
        adapter = new BusnessAdapter(getContext(),null);
        list.setAdapter(adapter);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                isDownRefresh = true;
                page = 1;
                getVpRightFromNet(page);
            }
            @Override
            public void onLoadMore(boolean isSlience) {
                isDownRefresh = false;
                ++page;
                getVpRightFromNet(page);
            }
        });
    }
    //商家
    private void getVpRightFromNet(int pageIndex) {
        String url= RequestCode.get_BusinessList;
        url += "&category="+category+"&area="+0+"&city="+2+"&order="+3+"&pageIndex="+pageIndex+"&memberID="+memBerId;
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.GETREQUEST,url,
                this, ShoplistBean.class,RequestCode.get_BusinessList);
        HttpHandleUtil.executeCommonGet(getContext(),httpRequestBean);
    }
    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (isDownRefresh){
            xRefreshView.stopRefresh();
        }else{
            xRefreshView.stopLoadMore();
        }
        if (data instanceof  ShoplistBean){
            ShoplistBean databean= (ShoplistBean) data;
            switch (databean.getResCode()){
                case "0000":
                    shopListData = databean.getData();
                    if (isDownRefresh){
                       adapter.clear();
                    }
                    adapter.append(shopListData);
                    break;
            }
        }

    }
    @Override
    public void onError(ErrorBean errorBean) {
    }
}
